package jmp3player;

import Dane.Settings;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import org.jdesktop.application.Action;

/**
 * Klasa wyświetla okno pozwalające na wybór opcji programu
 * @author lukeh
 * @version 1.0
 */
public class Ustawienia extends javax.swing.JDialog
  {

    /**
     * Uchwyt okna dialogowego wyboru plików
     */
    openDlg odlg;
    /**
     * Uchwyt klasy przechowującej ustawienia programu
     */
    Settings set;

    /**
     * Konstruktor
     * @param parent - uchwyt do rodzica
     * @param set - uchwyt klasy ustawień
     */
    public Ustawienia(java.awt.Frame parent, Settings set)
        {
        super(parent);

        initComponents();
        model.clear();
        this.set = set;
        refreshList();
        }

    /**
     * Dodaje folder do listy oraz do klasy opcji.
     * Korzysta z okna dialogowego openDlg
     */
    @Action
    public void addDir()
        {
        StringBuffer addr = new StringBuffer("");

        JFrame mainFrame = Jmp3PlayerApp.getApplication().getMainFrame();
        odlg = new openDlg(mainFrame, addr);
        odlg.setLocationRelativeTo(mainFrame);

        Jmp3PlayerApp.getApplication().show(odlg);

        set.addDir(addr.toString());
        refreshList();
        }

    /**
     * Kasuje z listy folder
     */
    @Action
    public void delDir()
        {
        System.out.println(lista.getSelectedIndex());
        set.foldery.removeElementAt(lista.getSelectedIndex());
        //model.remove(lista.getSelectedIndex());

        refreshList();
        }

    /**
     * Odświerza zawartość listy folderów zawartościa klasy opcji
     */
    private void refreshList()
        {
        model.clear();
        for (String s : set.foldery)
            {
            model.add(model.getSize(), s);
            }
        }

    /**
     * Zapisuje ustawienia do pliku a następnie zamyka okno opcji
     */
    @Action
    public void setApply()
        {
        set.saveSetFile();
        dispose();
        }

    /**
     * Zamyka okno opcji bez zapisania zmian
     */
    @Action
    public void cancel()
        {
        dispose();
        }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lista = new javax.swing.JList();
        dodaj = new javax.swing.JButton();
        usun = new javax.swing.JButton();
        zastosuj = new javax.swing.JButton();
        anuluj = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(jmp3player.Jmp3PlayerApp.class).getContext().getResourceMap(Ustawienia.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        model = new DefaultListModel();
        lista.setModel(model);
        lista.setName("lista"); // NOI18N
        jScrollPane1.setViewportView(lista);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(jmp3player.Jmp3PlayerApp.class).getContext().getActionMap(Ustawienia.class, this);
        dodaj.setAction(actionMap.get("addDir")); // NOI18N
        dodaj.setText(resourceMap.getString("dodaj.text")); // NOI18N
        dodaj.setName("dodaj"); // NOI18N

        usun.setAction(actionMap.get("delDir")); // NOI18N
        usun.setText(resourceMap.getString("usun.text")); // NOI18N
        usun.setName("usun"); // NOI18N

        zastosuj.setAction(actionMap.get("setApply")); // NOI18N
        zastosuj.setText(resourceMap.getString("zastosuj.text")); // NOI18N
        zastosuj.setName("zastosuj"); // NOI18N

        anuluj.setAction(actionMap.get("cancel")); // NOI18N
        anuluj.setText(resourceMap.getString("anuluj.text")); // NOI18N
        anuluj.setName("anuluj"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(230, 230, 230))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 371, Short.MAX_VALUE)
                        .addGap(17, 17, 17))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dodaj, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(usun, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(267, 267, 267))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(zastosuj)
                        .addGap(18, 18, 18)
                        .addComponent(anuluj)
                        .addGap(87, 87, 87))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dodaj)
                    .addComponent(usun))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(zastosuj)
                    .addComponent(anuluj))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton anuluj;
    private javax.swing.JButton dodaj;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList lista;
    private javax.swing.JButton usun;
    private javax.swing.JButton zastosuj;
    // End of variables declaration//GEN-END:variables
    DefaultListModel model;
    /**Model listy katalogów*/
  }
